package com.openarena.model.dao;

import com.openarena.model.Game;
import com.openarena.model.GameComment;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

public class GameCommentDAO extends InfArenaDAO {

    private static final Logger logger = Logger.getLogger(GameCommentDAO.class.getCanonicalName());

    public GameCommentDAO() {
        super();
    }

    public List<GameComment> getGameComments(Game game) {
        Session session = null;
        try {
            session = getSession();
            List<GameComment> comments = session.createCriteria(GameComment.class).add(Restrictions.eq("gameId", game.getId())).addOrder(Order.asc("dateCreated")).list();
            return comments;
        } catch (Exception e) {
            logger.log(Level.SEVERE, null, e);
            return Collections.EMPTY_LIST;
        } finally {
            close(session);
        }
    }

    public void saveComment(GameComment comment) {
        Session session = null;
        Transaction transaction = null;
        try {
            session = getSession();
            transaction = session.beginTransaction();
            session.saveOrUpdate(comment);
            transaction.commit();
        } finally {
            if (transaction != null && !transaction.wasCommitted()) {
                transaction.rollback();
            }

            close(session);
        }
    }
}
